// 导入数据库API
const db = require("../../config/db");
module.exports = {
    //加入购物车
    join_cart(product_id, user_id, product_img, product_name, product_num, product_price) {
        return new Promise((resolve, reject) => {
            let check_sql = `select * from shopping_cart where product_id="${product_id}" and user_id="${user_id}"`;
            db.query(check_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "购物车查询失败",
                        err,
                    });
                } else {
                    if (rows.length == 1) {
                        resolve({
                            code: -1,
                            msg: "商品已存在",
                        });
                    } else {
                        // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
                        let into_cart = `insert into shopping_cart(product_id,user_id,product_img,product_name,product_num,product_price) values("${product_id}","${user_id}","${product_img}","${product_name}","${product_num}","${product_price}")`;
                        db.query(into_cart, (err, rows) => {
                            if (err) {
                                reject({
                                    code: -1,
                                    msg: "加入购物车失败。",
                                    err,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "加入购物车成功。",
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    //获取购物车内容
    get_cart(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from shopping_cart where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取购物车信息失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取购物车信息成功。",
                        data: rows,
                    });
                }
            });
        });
    },
    //删除购物车
    delete_cart(product_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  shopping_cart where product_id = "${product_id}" and user_id="${user_id}" `;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "删除购物车商品失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "删除购物车商品成功。",
                    });
                }
            });
        });
    },

    //添加订单
    join_order(user_id, product_id, product_img, product_name, product_price, product_num, user_name, user_phone, user_address) {
        let order_type = 0;
        return new Promise((resolve, reject) => {
            let into_order = `insert into db_order(user_id,product_id,product_img,product_name,product_price,product_num,user_name,user_phone,user_address,order_type) values("${user_id}","${product_id}","${product_img}","${product_name}","${product_price}","${product_num}","${user_name}","${user_phone}","${user_address}","${order_type}")`;
            db.query(into_order, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "订单添加失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "订单添加成功。",
                    });
                }
            });
        });
    },
    //获取待收货商品列表
    get_not_received(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_order where user_id = "${user_id}" and order_type=0`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取待收货商品失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取待收货商品成功。",
                        data: rows,
                    });
                }
            });
        });
    },
    //修改待收货商品为已收货
    modify_received_state(user_id, product_id) {
        return new Promise((resolve, reject) => {
            let sql = `update db_order set order_type= 1 where  user_id= "${user_id}" and product_id=${product_id}`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "修改成功",
                        data: rows,
                    });
                }
            });
        });
    },
    //获取已收货商品列表
    get_to_received(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_order where user_id = "${user_id}" and order_type=1`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取已收货商品失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取已收货商品成功。",
                        data: rows,
                    });
                }
            });
        });
    },
    //获取订单内容
    get_order(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_order where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取订单失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取订单成功。",
                        data: rows,
                    });
                }
            });
        });
    },

    //添加地址
    join_address(user_id, user_name, user_phone, user_address) {
        return new Promise((resolve, reject) => {
            let into_order = `insert into db_address(user_id,user_name,user_phone,user_address) values("${user_id}","${user_name}","${user_phone}","${user_address}")`;
            db.query(into_order, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "地址添加失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "地址添加成功。",
                    });
                }
            });
        });
    },
    //获取地址列表
    get_address(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_address where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取地址失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取地址成功。",
                        data: rows,
                    });
                }
            });
        });
    },
    //判断商品收藏
    get_collect(user_id, product_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_collect where user_id = "${user_id}" and product_id="${product_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取收藏失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取收藏成功。",
                        data: rows,
                    });
                }
            });
        });
    },
    //添加商品收藏
    add_collect(user_id, product_id, product_img, product_name, product_price) {
        return new Promise((resolve, reject) => {
            let into_order = `insert into db_collect(user_id, product_id,product_img,product_name,product_price) values("${user_id}","${product_id}","${product_img}","${product_name}","${product_price}")`;
            db.query(into_order, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "添加收藏失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "添加收藏成功。",
                    });
                }
            });
        });
    },
    //取消商品收藏
    delete_collect(product_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_collect where product_id = "${product_id}" and user_id="${user_id}" `;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "取消收藏失败",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "取消收藏成功",
                    });
                }
            });
        });
    },
    //获取收藏列表
    get_collectlist(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_collect where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取收藏列表失败。",
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取收藏列表成功。",
                        data: rows,
                    });
                }
            });
        });
    },
};
